'''
 * CaRComp Execution Environment
 * 
 * Copyright (C) 2011 Taylor L. Riche
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
'''

import unittest
import filecmp
from carcomp.framework import connector, fake_endp
from carcomp.directory import localhost

class Test(unittest.TestCase):


    def test_instrument(self):
        conn = connector.InstrumentedConnector(fake_endp.generate_empty_endpoint(), fake_endp.generate_empty_endpoint())
        infile = open('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root), 'r')
        for msg in infile:
            conn.put(msg)
        conn.checkpoint('{}ft/tests/framework/CONN_OUT.txt'.format(localhost.local_directory.package_root))
        ret = filecmp.cmp('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root),
                          '{}ft/tests/framework/CONN_OUT.txt'.format(localhost.local_directory.package_root),
                          False)
        self.assertEqual(ret, True, 'Files not equal')
        
    def test_conversion(self):
        conn = connector.Connector(fake_endp.generate_empty_endpoint(), fake_endp.generate_empty_endpoint())
        conn = conn.instrument('{}ft/tests/framework/'.format(localhost.local_directory.package_root))
        self.assertEqual(conn._filename, '{}ft/tests/framework/i_FAKE-FAKE_o_FAKE-FAKE.txt'.format(localhost.local_directory.package_root))
        infile = open('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root), 'r')
        for msg in infile:
            conn.put(msg)
        conn.checkpoint()
        ret = filecmp.cmp('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root),
                          '{}ft/tests/framework/i_FAKE-FAKE_o_FAKE-FAKE.txt'.format(localhost.local_directory.package_root),
                          False)
        self.assertEqual(ret, True, 'Files not equal')

    def test_file_init(self):
        conn = connector.InstrumentedConnector(fake_endp.generate_empty_endpoint(), 
                                               fake_endp.generate_empty_endpoint(), 
                                               '{}ft/tests/framework/CONN_INIT_OUT.txt'.format(localhost.local_directory.package_root))
        infile = open('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root), 'r')
        for msg in infile:
            conn.put(msg)
        conn.checkpoint()
        ret = filecmp.cmp('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root),
                          '{}ft/tests/framework/CONN_INIT_OUT.txt'.format(localhost.local_directory.package_root),
                          False)
        self.assertEqual(ret, True, 'Files not equal')

    def test_file_override(self):
        conn = connector.InstrumentedConnector(fake_endp.generate_empty_endpoint(), 
                                               fake_endp.generate_empty_endpoint(), 
                                               '{}ft/tests/framework/CONN_INIT_OUT.txt'.format(localhost.local_directory.package_root))
        infile = open('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root), 'r')
        for msg in infile:
            conn.put(msg)
        conn.checkpoint('{}ft/tests/framework/CONN_OUT.txt'.format(localhost.local_directory.package_root))
        ret = filecmp.cmp('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root),
                          '{}ft/tests/framework/CONN_OUT.txt'.format(localhost.local_directory.package_root),
                          False)
        self.assertEqual(ret, True, 'Files not equal')

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_instrument']
    unittest.main()